package com.cw.config;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:Swagger2API文档的配置
 * @Author: wzw
 * @Time: 2020/11/20 10:35
 */
@Configuration
@EnableSwagger2
@ConditionalOnProperty(prefix = "swagger", name = "button-open", havingValue = "true")
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {
		List<Parameter> pars = new ArrayList<>();
		ParameterBuilder userName = new ParameterBuilder();
		ParameterBuilder userId = new ParameterBuilder();
		ParameterBuilder token = new ParameterBuilder();
		userName.name("userName").description("用户登录名").modelRef(new ModelRef("string")).parameterType("header");
		userId.name("userId").description("用户主键").modelRef(new ModelRef("string")).parameterType("header");
		token.name("token").description("token令牌").modelRef(new ModelRef("string")).parameterType("header");
		pars.add(userName.build());
		pars.add(userId.build());
		pars.add(token.build());

	  return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
									//为有@Api注解的Controller生成API文档
									.apis(RequestHandlerSelectors
										    .withClassAnnotation(Api.class))
									//为有@ApiOperation注解的方法生成API文档
									.apis(RequestHandlerSelectors
										    .withMethodAnnotation(
											  ApiOperation.class)).paths(
		    PathSelectors.any()).build().globalOperationParameters(pars);
    }

    private ApiInfo apiInfo() {
	  return new ApiInfoBuilder().title("床位管理系统").description("床位管理系统").version("1.0").build();
    }
}
